package com.hydl.power.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
  //判断选择的日期是否是本周
  public static boolean isThisWeek(long time)
  {
    Calendar calendar = Calendar.getInstance();
    int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
    calendar.setTime(new Date(time));
    int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
    if(paramWeek==currentWeek){
      return true;
    }
    return false;
  }
  //判断选择的日期是否是今天
  public static boolean isToday(long time)
  {
    return isThisTime(time,"yyyy-MM-dd");
  }
  //判断选择的日期是否是本月
  public static boolean isThisMonth(long time)
  {
    return isThisTime(time,"yyyy-MM");
  }
  private static boolean isThisTime(long time,String pattern) {
    Date date = new Date(time);
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    String param = sdf.format(date);//参数时间
    String now = sdf.format(new Date());//当前时间
    if(param.equals(now)){
      return true;
    }
    return false;
  }

  //将时间转换为时间戳
  public static String dateToStamp(String s) throws ParseException {
    String res;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
    Date date = simpleDateFormat.parse(s);
    long ts = date.getTime();
    res = String.valueOf(ts);
    return res;
  }


  public static void main(String[] args) throws ParseException {
    String time = "2021-04";
    long str = Long.valueOf(dateToStamp(time));
    System.out.println(str);
    System.out.println(isThisMonth(str));
  }
}

